﻿@model IEnumerable<MVC_No07.Models.TaskManagement>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("添加", "Create")
</p>
@using (Html.BeginForm("Index", "MyHome", FormMethod.Get)) { 
    @Html.DropDownList("UidList",null, htmlAttributes: new { @class = "form-control" })
    @Html.TextBox("Text",null, new { @class = "form-control" })
    <button type="submit">提交</button>
}
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.TaskPriority)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Content)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.AddTime)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.TaskState)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FinishedTime)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.uid)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr id="Task_@item.TaskID">
            <td>
                @switch (item.TaskPriority)
                {
                    case 1:
                        <span>高</span>
                        break;
                    case 2:
                        <span>中</span>
                        break;
                    case 3:
                        <span>低</span>
                        break;
                }
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Content)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AddTime)
            </td>
            <td>
                @if (item.TaskState == 1)
                {
                    <span>已完成</span>
                }
                else
                {
                    <span>未完成</span>
                }
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.FinishedTime)
            </td>
            <td>
                @{
                    var userInfoList = ViewBag.UserInfoList as Dictionary<int, string>;
                    var uid = userInfoList.FirstOrDefault(u => u.Key == item.uid).Value;
                }
                @uid
            </td>
            <td>
                <button onclick="Delete(@item.TaskID)">删除</button> |
                <button onclick="Update(@item.TaskID)">修改</button>
            </td>
        </tr>
    }

</table>
@section Scripts {
    <script src="~/Scripts/jquery-3.4.1.js"></script>
    <script>
        function Update(taskId) {
            location = "/MyHome/Update?uid=" + taskId;
        }

        function Delete(taskId) {
            if (confirm("确定要删除?")) {
                $.post("/MyHome/Delete", { uid: taskId }, function (data) {
                    switch (data) {
                        case 0:
                            alert("删除失败");
                            break;
                        case 1:
                            $("#Task_" + taskId).remove();
                            alert("删除成功");
                            break;
                        case 2:
                            alert("未知错误");
                            break;
                    }
                })
            }
        }
    </script>
}
